﻿using AkazoNobel.Scheduler.Core;
using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace AkazoNobel.Scheduler.StoreMasterData.Tasks
{
    public class CRMDataSync : BaseTask
    {
        /// <summary>
        /// 获取所有需要同步的数据列表
        /// </summary>
        /// <returns></returns>
        public List<CrmSyncModel> GetCrmSyncList()
        {
            List<CrmSyncModel> crmList = new List<CrmSyncModel>();

            try
            {
                crmList = CrmSyncService.GetSyncList();
                if (crmList != null)
                {
                    foreach (var item in crmList)
                    {
                        #region store_type_id
                        if (!string.IsNullOrEmpty(item.store_type_id))
                        {
                            if (item.store_type_id.Contains("DDCN"))
                            {
                                item.store_type_id = "1920001";
                            }
                            else if (item.store_type_id.Contains("DCSS"))
                            {
                                item.store_type_id = "1920002";
                            }
                            else if (item.store_type_id.Contains("DIS"))
                            {
                                item.store_type_id = "1920003";
                            }
                            else if (item.store_type_id.Contains("LCSS"))
                            {
                                item.store_type_id = "1920004";
                            }
                            else if (item.store_type_id.Contains("LIS"))
                            {
                                item.store_type_id = "1920005";
                            }
                            else if (item.store_type_id.Contains("MT"))
                            {
                                item.store_type_id = "1920006";
                            }
                            else if (item.store_type_id.Contains("HDC"))
                            {
                                item.store_type_id = "1920007";
                            }
                            else if (item.store_type_id.Contains("MASS"))
                            {
                                item.store_type_id = "1920008";
                            }
                            else if (item.store_type_id.Contains("MASS WITH TINTING"))
                            {
                                item.store_type_id = "1920009";
                            }
                            else if (item.store_type_id.Contains("OTHERS"))
                            {
                                item.store_type_id = "1920010";
                            }
                            else if (item.store_type_id.Contains("COMPETITOR"))
                            {
                                item.store_type_id = "1920011";
                            }
                            else if (item.store_type_id.Contains("E-COMMERCE"))
                            {
                                item.store_type_id = "1920012";
                            }
                            else if (item.store_type_id.Contains("EPS"))
                            {
                                item.store_type_id = "1920013";
                            }
                        }
                        #endregion
                        #region region_of_sales_id
                        if (item.region_of_sales_id.ToUpper() == "SOUTH")
                        {
                            item.region_of_sales_id = "50001";
                        }
                        else if (item.region_of_sales_id.ToUpper() == "EAST")
                        {
                            item.region_of_sales_id = "50002";
                        }
                        else if (item.region_of_sales_id.ToUpper() == "NORTH")
                        {
                            item.region_of_sales_id = "50003";
                        }
                        else if (item.region_of_sales_id.ToUpper() == "WEST")
                        {
                            item.region_of_sales_id = "50004";
                        }
                        #endregion
                        #region status_id
                        if (!string.IsNullOrEmpty(item.status_id))
                        {
                            if (item.status_id == "营业")
                            {
                                item.status_id = "110001";
                            }
                            else if (item.status_id == "关闭")
                            {
                                item.status_id = "110017";
                            }
                            else if (item.status_id.Contains("导入无状态"))
                            {
                                item.status_id = "110001";
                            }
                        }
                        #endregion
                        #region dispenser_type_id
                        if (item.dispenser_type_id == "BT-16")
                        {
                            item.dispenser_type_id = "1810001";
                        }
                        else if (item.dispenser_type_id == "D200")
                        {
                            item.dispenser_type_id = "1810002";
                        }
                        else if (item.dispenser_type_id == "TM300 1.1")
                        {
                            item.dispenser_type_id = "1810003";
                        }
                        else if (item.dispenser_type_id == "53PD")
                        {
                            item.dispenser_type_id = "1810004";
                        }
                        else if (item.dispenser_type_id == "53XD")
                        {
                            item.dispenser_type_id = "1810005";
                        }
                        else if (item.dispenser_type_id == "M3")
                        {
                            item.dispenser_type_id = "1810006";
                        }
                        else if (item.dispenser_type_id == "TM300 1.0")
                        {
                            item.dispenser_type_id = "1810007";
                        }
                        else if (item.dispenser_type_id == "TM280")
                        {
                            item.dispenser_type_id = "1810008";
                        }
                        else if (item.dispenser_type_id == "不需要")
                        {
                            item.dispenser_type_id = "1810009";
                        }
                        #endregion
                        #region pc_type_id
                        if (!string.IsNullOrEmpty(item.pc_type_id))
                        {
                            if (item.pc_type_id.Contains("新购"))
                            {
                                item.pc_type_id = "1830001";
                            }
                            else if (item.pc_type_id == "无")
                            {
                                item.pc_type_id = "1830003";
                            }
                            else
                            {
                                item.pc_type_id = "1830002";
                            }
                        }
                        #endregion
                        #region shaker_type_id
                        if (item.shaker_type_id == "SHH-IV")
                        {
                            item.shaker_type_id = "1820001";
                        }
                        else if (item.shaker_type_id == "SO-40A")
                        {
                            item.shaker_type_id = "1820002";
                        }
                        else if (item.shaker_type_id == "Vibro")
                        {
                            item.shaker_type_id = "1820003";
                        }
                        else if (item.shaker_type_id == "三华S5")
                        {
                            item.shaker_type_id = "1820004";
                        }
                        else if (item.shaker_type_id == "不需要")
                        {
                            item.shaker_type_id = "1820005";
                        }
                        #endregion
                        #region delivery_type
                        if (item.delivery_type == "新机安装(STD)")
                        {
                            item.delivery_type = "1800001";
                        }
                        else if (item.delivery_type == "旧机转让(STD)")
                        {
                            item.delivery_type = "1800002";
                        }
                        else if (item.delivery_type == "MT Automatic Machine Move(STD)")
                        {
                            item.delivery_type = "1800003";
                        }
                        else if (item.delivery_type == "MT Manual Manchine Move(STD)")
                        {
                            item.delivery_type = "1800004";
                        }
                        else if (item.delivery_type == "新机安装(DIR)")
                        {
                            item.delivery_type = "1800005";
                        }
                        else if (item.delivery_type == "旧机转让(DIR)")
                        {
                            item.delivery_type = "1800006";
                        }
                        else if (item.delivery_type == "无需机器")
                        {
                            item.delivery_type = "1800007";
                        }
                        #endregion
                        #region tinting_status_id
                        if (!string.IsNullOrEmpty(item.tinting_status_id))
                        {
                            if (item.tinting_status_id.Contains("新建"))
                            {
                                item.tinting_status_id = "1790001";
                            }
                            else if (item.tinting_status_id.Contains("使用"))
                            {
                                item.tinting_status_id = "1790002";
                            }
                            else if (item.tinting_status_id.Contains("经销商寄存"))
                            {
                                item.tinting_status_id = "1790003";
                            }
                            else if (item.tinting_status_id.Contains("公司大仓"))
                            {
                                item.tinting_status_id = "1790004";
                            }
                            else if (item.tinting_status_id.Contains("报废"))
                            {
                                item.tinting_status_id = "1790005";
                            }
                            else if (item.tinting_status_id.Contains("旧机更换"))
                            {
                                item.tinting_status_id = "1790006";
                            }
                        }

                        #endregion
                        #region store_close_reason_id
                        if (!string.IsNullOrEmpty(item.store_close_reason_id))
                        {
                            switch (item.store_close_reason_id)
                            {
                                case "A.租金提高":
                                    item.store_close_reason_id = "1950001";
                                    break;
                                case "B.运营原因":
                                    item.store_close_reason_id = "1950002";
                                    break;
                                case "C.市场原因":
                                    item.store_close_reason_id = "1950003";
                                    break;
                                case "D.渠道调整":
                                    item.store_close_reason_id = "1950004";
                                    break;
                                case "E.店面到期":
                                    item.store_close_reason_id = "1950005";
                                    break;
                                case "F.店面升级":
                                    item.store_close_reason_id = "1950006";
                                    break;
                                case "G.店面迁址":
                                    item.store_close_reason_id = "1950007";
                                    break;
                                default:
                                    break;
                            }
                        }
                        #endregion
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            return crmList;
        }


        /// <summary>
        /// post json字符串到CRM 接口
        /// </summary>
        /// <param name="syncData">需要同步的数据列表</param>
        /// <returns></returns>
        public HttpResult PostDataToCrm(List<CrmSyncModel> syncData)
        {
            HttpResult result = new HttpResult();
            string postJson = CrmSyncService.List2Json(syncData);
            Logger.Error(postJson);

            if (postJson != string.Empty)
            {
                result = CrmSyncService.RestPost(postJson);
            }

            return result;
        }

        public override void ExecuteTask(Quartz.IJobExecutionContext context)
        {
            //execute procedure
            string connectionName = Constant.SMD_DB_NAME;
            SqlConnectionHelper.RunProcedure("SMDtoCRMsync", new SqlParameter[] { }, 600, connectionName);

            List<CrmSyncModel> syncModelList = new List<CrmSyncModel>();
            syncModelList = GetCrmSyncList();

            HttpResult result = PostDataToCrm(syncModelList);

            if (result.StatusCode == System.Net.HttpStatusCode.OK)
            {
                CrmSyncService.UpdateSyncStatus(syncModelList, SyncStatus.Success, result.StatusDescription);
            }
            else
            {
                CrmSyncService.UpdateSyncStatus(syncModelList, SyncStatus.Failed, result.StatusDescription);
            }
        }
    }
}
